package com.honglang.aop.impl;



import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.web.context.WebApplicationContext;

import com.honglang.module.album.service.PhotoService;
import com.honglang.service.FileSysService;
import com.honglang.struts2.action.UploadFileAction;






@Aspect
public class FileUploadAspect {
	private static final Logger logger = Logger.getLogger(FileUploadAspect.class);
	
	@Pointcut("execution(* com.honglang..action.*.*(..))")
	protected void fileSysAndPhotoPointCut(){};
	
	
	@After("fileSysAndPhotoPointCut() && " +"@annotation(com.honglang.aop.impl.UpdateFileSysAndPhotoInDB)")
	public void addFileAndPhotoToDB(JoinPoint jp){
		logger.info("Begin add upload file message to database after file upload");
		UploadFileAction uploadAction = (UploadFileAction)jp.getTarget();
		WebApplicationContext  webApplicationContext = uploadAction.getWebApplicationContext();
		String cotextRealPath = uploadAction.getSession().getServletContext().getRealPath("/");
		PhotoService photoService = (PhotoService) webApplicationContext.getBean("photoService");
		photoService.addPhoto(cotextRealPath, uploadAction.getFileListFileName());
		FileSysService fileSysService = (FileSysService)webApplicationContext.getBean("fileSysService");
		fileSysService.addFileSys(cotextRealPath,  uploadAction.getFileListFileName());
		logger.info("Add upload file message to database done !");
	}
}
